package com.asia5b.a5mall.order.activity

import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.asia5b.a5bmall.resource.ActCons
import com.asia5b.a5mall.middleware.base.SimpleActivity
import com.asia5b.a5mall.middleware.logic.viewmodel.order.AftersaleActivityVM
import com.asia5b.a5mall.middleware.model.base.ResultModel
import com.asia5b.a5mall.middleware.model.order.AftersaleModel
import com.asia5b.a5mall.middleware.utils.push
import com.asia5b.a5mall.order.adapter.AftersaleAdapter
import com.asia5b.a5mall.order.databinding.ActivityAftersaleBinding
import com.scwang.smartrefresh.layout.api.RefreshLayout
import kotlinx.android.synthetic.main.activity_aftersale.*

/**
 * Created by zhangyuncai on 2020/1/13.
 * 售后列表
 */
@Route(path = ActCons.AftersaleListActivity)
class AftersaleActivity : SimpleActivity<AftersaleActivityVM, ActivityAftersaleBinding>() {

    override val viewModelClazz: Class<AftersaleActivityVM>
        get() = AftersaleActivityVM::class.java

    override fun getDataBinding(): ActivityAftersaleBinding {
        return ActivityAftersaleBinding.inflate(layoutInflater)
    }


    override var sIsDark = true
    val mRecyclerView by lazy { rv_aftersale }
    val mDatas = arrayListOf<AftersaleModel>()
    val mAdapter by lazy { AftersaleAdapter(mDatas) }

    override fun initView() {
        mSmartRefreshLayout=mDataBinding.autoAftersale
        mRecyclerView.run {
            layoutManager = LinearLayoutManager(mActivity)
            adapter = mAdapter
        }
        getDatas(0)
    }

    override fun initEvent() {
        mAdapter.setOnItemClickListener { adapter, view, position ->
            val aftersaleModel = mAdapter.getItem(position) as AftersaleModel
            push(ActCons.AftersaleDetailActivity, {
                it.withString("sn", aftersaleModel.sn)
            })
        }
    }

  override  fun startObserver()
    {
        mViewModel.getDatasResultLiveData.observe(this, Observer {
            getDatasResult(it)
        })
    }
    override fun initData() {

    }
    fun getDatas(refresh: Int) {
        mViewModel.getDatas(refresh)
    }


     fun getDatasResult(resultModel: ResultModel<ArrayList<AftersaleModel>>) {
        if (resultModel.page <= 1) {
            mDatas.clear()
        }
        if (!resultModel.data.isNullOrEmpty()) {
            mAdapter.addData(resultModel.data!!)
        }
        mDataBinding.autoAftersale.setEnableLoadMore(resultModel.page < resultModel.sumPage)
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        getDatas(2)
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        getDatas(1)
    }
}